package team.yidong.fenda.wx.Service.Impl;

import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.yidong.fenda.entity.*;
import team.yidong.fenda.repository.AnswerRepository;
import team.yidong.fenda.repository.MasterRepository;
import team.yidong.fenda.repository.MasterScoreRepository;
import team.yidong.fenda.repository.QuestionRepository;
import team.yidong.fenda.wx.Service.WxAnswerService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by xuanlong on 2017/11/10.
 */
@Service
public class WxAnswerServiceImpl implements WxAnswerService {
    @Autowired
    QuestionRepository questionRepository;
    @Autowired
    MasterRepository masterRepository;
    @Autowired
    AnswerRepository answerRepository;
    @Autowired
    MasterScoreRepository masterScoreRepository;

    @Transactional
    @Override
    public void addAnswer(Integer questionId, String wxNumber, String info, String recordUrl, String imgUrl, String createDate, String recordTime, String answerTime) throws ParseException {
        Question question = questionRepository.findOne(questionId);
        Master master = masterRepository.findByOpenId(wxNumber);
        Answer answer = new Answer();
        createDate = createDate.replace("/", "-");
        answer.setCreatDate(createDate);
        answer.setIsBlack("否");
        answer.setRecordTime(recordTime);
        answer.setAnswerTime(answerTime);
        answer.setPraiseImg1("https://qny.liuxuanlong.xyz/QQ%E5%9B%BE%E7%89%8720171209151703.png");
        answer.setPraiseImg2("https://qny.liuxuanlong.xyz/QQ%E5%9B%BE%E7%89%8720171209151712.png");
        answer.setStarNum(0);
        answer.setListenTime(0);
        //如果是第一个回答的，设置question的answerTime
        if (question.getAnswerList().size() == 0) {
            SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            long from = simpleFormat.parse(question.getCreatDate()).getTime();
            long to = simpleFormat.parse(createDate).getTime();
            int minutes = (int) ((to - from) / (1000 * 60));
            question.setAnswerTime(Integer.toString(minutes));
            question.setIsSolved("是");
        }

        if (info != null && !info.equals("")) {
            answer.setInfo(info);
        }
        if (recordUrl != null && !recordUrl.equals("")) {

            recordUrl = recordUrl.replace("wxfile:", "https://");
            recordUrl = recordUrl.substring(0, 8) + "qny.liuxuanlong.xyz/" + recordUrl.substring(8);
            answer.setRecordUrl(recordUrl);
        }
        Set<AnswerImg> answerImgSet = new HashSet<>();
        if (imgUrl != null && !imgUrl.equals("")) {
            JSONArray jsonArray = JSONArray.fromObject(imgUrl);
            for (int i = 0; i < jsonArray.size(); i++) {
                String str = (String) jsonArray.get(i);
                str = str.replace("wxfile:", "https://");
                str = str.substring(0, 8) + "qny.liuxuanlong.xyz/" + str.substring(8);
                AnswerImg answerImg = new AnswerImg();
                answerImg.setImgUrl(str);


                answerImgSet.add(answerImg);
            }
            answer.setAnswerImgSet(answerImgSet);
        }

        answer.setIsRead("0");
        answer.setQuestion(question);
        answer.setMaster(master);


        answerRepository.save(answer);
        if ("免费".equals(question.getWxOrder().getHandleStatus())) {
            SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            MasterScore masterScore = new MasterScore();
            masterScore.setStatus("免费问题无积分");
            masterScore.setNum(0.0);
            masterScore.setCreatDate(simpleFormat.format(new Date()));
            masterScore.setMasterScoreAnswer(answer);
            masterScoreRepository.save(masterScore);
        } else {
            MasterScore masterScore = new MasterScore();
            masterScore.setStatus("初始积分");
            masterScore.setNum(0.0);
            SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            masterScore.setCreatDate(simpleFormat.format(new Date()));
            masterScore.setMasterScoreAnswer(answer);
            masterScoreRepository.save(masterScore);
        }


    }

    @Override
    @Transactional
    public void starAnswer(Integer answerId) {
        Answer answer = answerRepository.findOne(answerId);
        answer.setStarNum(1);
        answerRepository.save(answer);
    }
}
